<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/07
 * Time: 15:23
 */

namespace HiCommon\Model;

class OrderRepairstore extends Base
{
    
    protected $table = 'order_repairstore';
    
    protected $guarded = [];
    
    //public $timestamps = false;

    //一对一关联门店
    public function shop()
    {
        return $this->hasOne(Shop::class, 'id','shop_id')
            ->select('id','logo','name','address','phone','area','longitude','latitude', 'logo');
    }

    //关联服务商品
    public function goods()
    {
        return $this->hasOne(RepairstoreGoods::class, 'id','repairstore_goods_id')
            ->with(['pic'])
            ->select('id','price','name');
    }

    //关联服务商品图片
    public function repair_goods_pic()
    {
        return $this->hasOne(RepairstoreGoodsImg::class, 'goods_id','repairstore_goods_id')
            ->select('id','goods_id','picurl');
    }

    //关联服务商品
    public function repairstore_goods(){
        return $this->belongsTo(RepairstoreGoods::class, 'repairstore_goods_id')
            ->select('id','name');
    }


    //关联服务商品
    public function to_goods()
    {
        return $this->hasOne(RepairstoreGoods::class, 'id','repairstore_goods_id');
    }

    //商品类型
    public function gtype()
    {
        return $this->hasOne(RepairstoreType::class, 'id','goods_type')
            ->select('id','title');
    }

     //用户关联
    public function user()
    {
        return $this->belongsTo(Users::class, 'user_id')
            ->select('id', 'nickname', 'phone', 'realname', 'email','sex','head_img');
    }

    //支付记录表
    public function orderPayWechat()
    {
        return $this->hasOne(OrderPay::class, 'order_sn','order_number')
            ->select('order_sn','out_trade_no','total_fee');
    }

    //一对多
    public function maintain_goods()
    {
        return $this->hasMany(OrderRepairstoreGoods::class, 'order_repairstore_id','id')
            ->with(['get_goods','get_product'])
            ->select('id','order_repairstore_id','goods_id','product_id','goods_num','goods_price','goods_amount');
    }


    //用户评论
    public function order_apprise()
    {
        return $this->hasOne(OrderRepairstoreApprise::class, 'order_id','id')
            ->select('id','order_id','score','content');
    }

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];
    
    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }
    
        /**
         * 获取图片可用链接
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getImgUrlAttribute($value)
        {
            $arr_data = [
              'src'=>$value,
              'full_src'=>oss_full_url($value),
            ];
            return $arr_data;
        }
        
        /**
         * 扩展信息
         *
         * @return array
         * @author wumengmeng <wu_mengmeng@foxmail.com>
         */
        public function getExtendAttribute()
        {
            $s_created_at = $this->created_at;
            $arr_data = [
              'created_at'=>ymd_friendly_date($s_created_at),
            ];

            return $arr_data;
        }

    
  
}